/**
 * Project Name:kp-common
 * File Name:PasswordHelper.java
 * Package Name:com.kp.common.utils
 * Date:2016年5月3日下午6:23:53
 * Copyright (c) 2016, chenzhou1025@126.com All Rights Reserved.
 */
package com.md.common.utils;

import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

/**
 * 功能描述：密码帮助类
 *
 * @author lujianping@oak168.com
 * @version 1.0
 * @date 2018-6-19 16:59
 * @since JDK 1.8
 */
public class PasswordHelper {

    private RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
    /**
     * 加密方式
     */
    private String hashAlgorithmName = "md5";

    /**
     * 加密次数
     */
    private int hashIterations = 1;

    public void setRandomNumberGenerator(RandomNumberGenerator randomNumberGenerator) {
        this.randomNumberGenerator = randomNumberGenerator;
    }

    public void setHashAlgorithmName(String hashAlgorithmName) {
        this.hashAlgorithmName = hashAlgorithmName;
    }

    public void setHashIterations(int hashIterations) {
        this.hashIterations = hashIterations;
    }

    /**
     * 生产用户密码加密盐
     *
     * @return
     */
    public String createSalt() {
        return randomNumberGenerator.nextBytes().toHex();
    }

    /**
     * 加密密码
     *
     * @param @param  password
     * @param @param  salt
     * @param @return
     * @return String
     * @throws
     */
    public String encryptPassword(String password, String salt) {
        String newPassword = new SimpleHash(hashAlgorithmName, password, ByteSource.Util.bytes(salt), hashIterations).toHex();
        return newPassword;
    }


    public String encryptPassword(String password) {
        return new SimpleHash(hashAlgorithmName, password).toHex();
    }

    public static void main(String[] args){
        System.out.println(ByteSource.Util.bytes("123456lkw"));
        System.out.println(new PasswordHelper().encryptPassword("123456","demo"));
        System.out.println(new Md5Hash("123456","123456").toString());
        System.out.println(new SimpleHash("md5", "123456", ByteSource.Util.bytes("3c02d4191fa606a4d6ca370e9c5bd191lkw3"), 3));
    }
}
